package com.study.config;

import org.aopalliance.intercept.MethodInterceptor;
import org.springframework.aop.ClassFilter;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.NameMatchMethodPointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;
import org.springframework.util.PatternMatchUtils;

import com.study.comp.HelloAop;
import com.study.comp.IHello;
import com.study.comp.MyAdvice;

@Configurable
public class RootConfig {
	
	@Autowired MethodInterceptor myAdvice;
	@Autowired NameMatchMethodPointcut myPointcut;
	@Autowired DefaultPointcutAdvisor myAdvisor;

	@Bean
	public IHello iHello(){
		return new HelloAop();
	}
	
	@Bean
	public MethodInterceptor myAdvice(){
		return new MyAdvice();
	}
	
	@Bean
	public NameMatchMethodPointcut myPointcut(){
		NameMatchMethodPointcut pointcut = new NameMatchMethodPointcut();
		
		ClassFilter classFilter = new ClassFilter() {
			public boolean matches(Class<?> clazz) {
				String mappedName = "Hello*";
				return PatternMatchUtils.simpleMatch(mappedName , clazz.getSimpleName());
			}
		};
		pointcut.setClassFilter(classFilter);
		pointcut.setMappedName("do*");
		return pointcut;
	}
	
	@Bean
	public DefaultPointcutAdvisor myAdvisor(){
		return new DefaultPointcutAdvisor(myPointcut,myAdvice);
	}
	
	@Bean
	public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator(){ 
		return new DefaultAdvisorAutoProxyCreator();
	}
	
	/**
	 * 
	@Bean
	public ProxyFactoryBean myPf(){
		ProxyFactoryBean pf = new ProxyFactoryBean();
		pf.addAdvisor(myAdvisor);
		return pf;
	}
	 * 
	 */
}
